package swd.deploy.frame_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
   QueryBean gqbnDAO=null;
   public String action(javax.servlet.jsp.PageContext pageContext){
      HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
      HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();

      View view=(View)request.getAttribute("view");
      (new WebFunction()).setPortal_to_session(request); //set session from portal
      if (request.getSession().getAttribute("u_id")==null) {
         try{
            request.getRequestDispatcher((new StringBuffer("login.jsp")).insert(0,"/").toString()).forward(request,response);
            return "login";
         }
         catch(Exception le){
            view.setOnloadScript("can not redirect to login page...please contact IT");
            return "";
         }
      }
      //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
      gqbnDAO=new QueryBean(view.getDBAlias(),view.getDBDebug(),view.getSourceCharset(),view.getTargetCharset());

      DataView DataView=null;
      DataViewForm DataViewForm=null;

      DataView = new DataView(view.getDBAlias(),view.getDBDebug(), view.getSourceCharset(), view.getTargetCharset());
      DataViewForm = new DataViewForm();

      request.setAttribute("DataView",DataView);//讓前端可以呼叫dataview
      request.setAttribute("DataViewForm",DataViewForm);


      Business business=new Business(gqbnDAO);
      //Flow flow=new Flow(gqbnDAO,lstrUserNO,"AFS",view,business);

      //String lstrResult=null;
      String strFrameNo="";
      String strResult="";

      try{

          if(view.getframe_no()!=null && view.getframe_no().trim().length()>0){
             view.setSelectShow("");
             strFrameNo=view.getframe_no().trim();
          } else {
             view.setSelectShow("none");
              view.setTipMessage("請輸入已存在【Frame編號】後按【查詢】或按【新增】產生新的Frame編號");
          }

          view.setButtonVisible("0");


         if(view.getPrgAction()!=null && view.getPrgAction().equals("query") && view.getframe_no()!=null){
            strResult= business.selectTableDataToFld(view);
            if (strResult.equals("查無資料")){
               view.setAllClear();
               view.setButtonVisible("0");
               view.setframe_no(strFrameNo);
               view.setSelectShow("none");
               view.setOnloadScript("alert('查無資料');");
            } else {
               strResult=business.checkCanModifyFrameNo(view) ;
               if (strResult.equals("0")){
                  view.setOnloadScript("alert('此Frame No不能修改!!');");
                  view.setAllClear();
                  view.setSelectShow("none");
                  view.setPrgStatus("exit");
               } else {
                  view.setTipMessage("修改Frame名稱、系統群組後按【存檔】");
                  view.setBtnQueryShow("none");
                  view.setBtnAddShow("none");
                  view.setBtnSaveShow("");
                  view.setframe_no_readonly(true);
               }
            }
            view.setPrgStatus("query");
         }

         if(view.getPrgAction()!=null && view.getPrgAction().equals("add")){
            view.setAllClear();
            //flow.preAddAction(request.getSession().getAttribute("u_id").toString());
            view.setPrgStatus("add");
            view.setSelectShow("none");
            view.setButtonVisible("1");
         }

         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("add")){
            view.setTipMessage("請輸入下列欄位，星號為必填欄位。輸入完畢請按存檔");
            //view.setOnloadScript("alert('" + flow.addAction() + "');");
            view.setButtonVisible("2");
            view.setframe_no_readonly(true);
            view.setSelectShow("save");
         }

         // 修改存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("query")){
            view.setOnloadScript("alert('" + business.exeUpdSQL(view) + "');");
            view.setButtonVisible("2");
            view.setframe_no_readonly(true);
            view.setPrgStatus("save");
         }

         // 修改存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("save") && view.getPrgStatus().equals("save")){
            view.setOnloadScript("alert('" + business.exeUpdSQL(view) + "');");
            //business.getTypePathData(view);
            view.setButtonVisible("2");
            view.setframe_no_readonly(true);
            view.setPrgStatus("save");
         }

         //按整批存檔
         if(view.getPrgAction()!=null && view.getPrgAction().equals("saveAll")){
             if(view.getrowsn()==null || view.getrowsn().equals("")){
                 view.setOnloadScript("alert('沒有修改!');");
             } else{
                  view.setOnloadScript("alert('"+business.batchUpdateData(view)+"');");
             }
             view.setButtonVisible("2");
             view.setframe_no_readonly(true);
             view.setPrgStatus("save");
         }




         // 離開
         if(view.getPrgAction()!=null && view.getPrgAction().equals("exit")){
             if (view.getPrgStatus()!=null && (view.getPrgStatus().equals("add")||view.getPrgStatus().equals("modify"))){
                 view.setAllClear();
                 view.setButtonVisible("0");
                 view.setframe_no(strFrameNo);
                 view.setPrgStatus("exit");
             } else {
                view.setAllClear();
                view.setSelectShow("none");
                view.setPrgStatus("exit");
             }
         }



         business.setDataViewForm(DataViewForm,view,request.getSession().getAttribute("u_id").toString());

         return "";
      }
      catch(Exception e){
         view.setOnloadScript("alert('Program error please contact IT.');");
         return "";
      }
      finally{
         try{
            if(gqbnDAO!=null){
               gqbnDAO.close();
            }
         }
         catch(Exception e){
            System.out.println("can not close querybean in Action.java");
         }
      }
   }
}
